package com.hexu.event.dao;

import com.hexu.event.entity.Consulting;
import com.hexu.event.entity.Reply;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ReplyDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    public boolean addReply(Reply reply){
        String sql = "insert into reply(reply_account,reply_time,account)"
                + " values (?,?,?)";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, reply.getReplyAccount());
            ps.setString(2, reply.getReplyTime());
            ps.setString(3, reply.getAccount());
            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }
        //添加失败
        return false;
    }


    //返回回复信息
    public List<Reply> getReply(String account, String replyAccount){
        List<Reply> replyList = new ArrayList<>();
        String sql = "select * from reply where account=? and reply_account=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            ps.setString(2, replyAccount);
            rs = ps.executeQuery();

            while (rs.next()){
                Reply reply = new Reply();
                reply.setId(rs.getInt("id"));
                reply.setReplyTime(rs.getString("reply_time"));
                reply.setReplyAccount(rs.getString("reply_account"));
                reply.setAccount(rs.getString("account"));

                replyList.add(reply);
            }
            return replyList;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }
}
